<%@ page contentType="text/html; charset=UTF-8" language="java" %>
<%@ include file="include.jsp" %>
<%@ taglib prefix="sql" uri="http://java.sun.com/jsp/jstl/sql"  %>
<script type="text/javascript" src="../js/jquery-1.3.2.min.js"></script>
<style type="text/css">

    .button {
        cursor: pointer;
        background-color: #FFCC00;
        font-weight: bold;
        width: 100px;

    }

    .newAppTable {
        width: 490px;
        background-color: skyblue;
    }

    #appName {
        width: 200px;
    }

    .newAppTable td {
        font-size: x-small;
        font-weight: bold;
    }

    .required {
        color: red;
    }

</style>

<sql:setDataSource
        var="dataSource"
        driver="com.mysql.jdbc.Driver"
        url="jdbc:mysql://localhost:3306/diplom6"
        user="root"
        password="root"
/>
<%--url="jdbc:mysql://mysql37.1gb.ru:3306/gb_gob"  --%>
 <%--user="gb_gob"--%>
        <%--password="a945a3b3"--%>



<c:if test="${param.appId ne null && !empty param.appId }">
    <sql:query var="application" dataSource="${dataSource}" >
               select * from applications where applicationId = ?
        <sql:param value="${param.appId}" />
    </sql:query>
    <c:set var="name" value="${application.rows[0].applicationName}"/>
    <c:set var="descr" value="${application.rows[0].description}"/>
    <c:set var="statusId" value="${application.rows[0].statusId}"/>
    <sql:query var="status" dataSource="${dataSource}" >
               select statusName from statuses where statusId = ?
        <sql:param value="${statusId}" />
    </sql:query>

    <c:set var="statusName" value="${status.rows[0].statusName}"/>


</c:if>

<table class="newAppTable">
    <tr>
        <td>Имя приложения <span class="required">*</span></td>
        <td><input type="text" name="appName" id="appName" value="${name}"></td>
        <td width="30%" class="required"></td>
    </tr>
    <tr>
        <td>Описание</td>
        <td><textarea rows="5" cols="22" id="appDescription"><c:out value="${descr}"/></textarea></td>
        <td></td>
    </tr>
    <tr>
        <td>Статус <span class="required">*</span></td>
        <td>
            Активно <input type="radio" name="status" value="active"
                <c:if test="${statusName eq 'active'}">checked</c:if>
                ><br>
            Нективно <input type="radio" name="status" value="inactive" id="status"
                <c:if test="${statusName eq 'inactive'}">checked</c:if>
                >
            <input type="hidden" value="${param.categoryId}" id="category">
            <input type="hidden" value="${param.appId}" id="appId">

        </td>
        <td class="required"></td>
    </tr>
    <tr>
        <td></td>
        <td><input type="button" value="Сохранить" id="add" class="button">&nbsp;<input type="button"
                                                                                        onclick="tb_remove()"
                                                                                        value="Отмена" class="button">
        </td>
        <td></td>
    </tr>
</table>

<script type="text/javascript">


     $("#add").click(function() {

            $(".required").html('');
            var appName = $("#appName").val();
            var categoryId = $("#category").val();
            var appId = $("#appId").val();
            var description = $("#appDescription").val();
            var status = $("input:radio:checked").attr("value");

            $.post("newApplication.htm", { appName: appName, categoryId: categoryId, description: description, status: status, appId : appId },
                    function(data) {
                        if (data == '') {
                            window.location = 'createCategory.htm';
                        } else {
                            var array = data.split(';')
                            for (i = 0; i < array.length; i++) {
                                var t = array[i].split(":");
                                $("#" + t[0]).parent().next().html(t[1]);
                            }
                        }
                    });

        });

</script>



